﻿using System.Windows.Forms;
using System;
using Sockets;

namespace server
{
    public partial class frmMain : Form
    {
        #region Member Variables
        // The Socket that will listen for connections
        ServerSocket server = new ServerSocket();

        private delegate void simpleDelegate();
        private delegate void stringDelegate(string data);
        #endregion

        // Required to keep things thread safe
        #region Subscribers
        private void clientConnected(object sender, EventArgs e)
        {
            this.Invoke(new simpleDelegate(clientConnectedCallback));
        }

        private void clientDisconnected(object sender, EventArgs e)
        {
            this.Invoke(new simpleDelegate(clientDisconnectedCallback));
        }

        private void dataReceived(object sender, DataReceivedEventArgs e)
        {
            this.Invoke(new stringDelegate(dataReceivedCallback), e.Data);
        }
        #endregion // This is all required to keep things thread safe.

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.AcceptButton = btnSend;

            // Subscribe to the ServerSocket's events
            server.OnClientConnected += new System.EventHandler(clientConnected);
            server.OnClientDisconnected += new System.EventHandler(clientDisconnected);
            server.OnDataReceived += new ServerSocket.DataReceivedHandler(dataReceived);
        }

        private void cmdListen_Click(object sender, System.EventArgs e)
        {
            if (btnListen.Text.Equals("Listen"))
            {
                if (txtPort.Text == "")
                {
                    MessageBox.Show("Please enter a port number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                int port = -1;
                try
                {
                    port = int.Parse(txtPort.Text);
                }
                catch (Exception)
                {
                    MessageBox.Show("Please enter a valid port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                btnListen.Text = "Stop";
                txtPort.Enabled = false;
                this.Text = "Server - 0 connections";
                server.LocalPort = port;
                server.Listen();
            }
            else
            {
                btnListen.Text = "Listen";
                txtPort.Enabled = true;
                this.Text = "Server - Stopped";
                server.Disconnect();
            }
        }

        private void cmdSend_Click(object sender, EventArgs e)
        {
            if (txtSend.Text.Length != 0)
            {
                server.send(txtSend.Text);
                txtMessages.Text += "YOU: " + txtSend.Text + Environment.NewLine;
                txtSend.Text = "";
            }
        }

        private void clientConnectedCallback()
        {
            txtSend.Enabled = true;
            btnSend.Enabled = true;

            this.Text = "Server - " + server.ConnectedClients + " connection";

            if(server.ConnectedClients != 1)
                this.Text += "s";
        }

        private void clientDisconnectedCallback()
        {
            if (server.ConnectedClients == 0)
            {
                txtSend.Enabled = false;
                btnSend.Enabled = false;
            }

            this.Text = "Server - " + server.ConnectedClients + " connection";

            if (server.ConnectedClients != 1)
                this.Text += "s";
        }

        private void dataReceivedCallback(string data)
        {
            txtMessages.Text += "THEM: " + data + Environment.NewLine;
        }
    }
}
